<script setup>
import { ref, reactive } from 'vue'
import { getCategoryList, addCategory, updateCategory, deleteCategory } from '@/apis/category'
import { ElMessage, ElMessageBox } from 'element-plus'

const categoryList = ref([])
const loading = ref(false)
const total = ref(0)
const page = ref(1)
const pageSize = ref(10)

const dialogVisible = ref(false)
const dialogTitle = ref('')
const form = reactive({
  id: null,
  name: ''
})
const formRef = ref()
const search = ref('')

// 时间格式化
function formatDate(val) {
  if (!val) return ''
  const d = new Date(val)
  return d.toLocaleString()
}

// 获取分类列表
const fetchCategoryList = async () => {
  loading.value = true
  try {
    const res = await getCategoryList({ page: page.value, pageSize: pageSize.value, name: search.value })
    if (res.code === 1 && res.data) {
      categoryList.value = res.data.list || []
      total.value = res.data.total || 0
    } else {
      categoryList.value = []
      total.value = 0
    }
  } finally {
    loading.value = false
  }
}

// 打开新增/编辑弹窗
const openDialog = (row = null) => {
  if (row) {
    dialogTitle.value = '编辑分类'
    form.id = row.id
    form.name = row.name
  } else {
    dialogTitle.value = '新增分类'
    form.id = null
    form.name = ''
  }
  dialogVisible.value = true
}

// 提交表单
const handleSubmit = async () => {
  if (!form.name) {
    ElMessage.error('分类名不能为空')
    return
  }
  try {
    if (form.id) {
      await updateCategory({ id: form.id, name: form.name })
      ElMessage.success('编辑成功')
    } else {
      await addCategory({ name: form.name })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
    fetchCategoryList()
  } catch (e) {
    ElMessage.error('操作失败')
  }
}

// 删除分类
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该分类吗？', '提示', { type: 'warning' })
    await deleteCategory({ id: row.id })
    ElMessage.success('删除成功')
    fetchCategoryList()
  } catch (e) {
    // 取消或失败
  }
}

fetchCategoryList()
</script>
<template>
  <div>
    <h2>分类管理</h2>
    <div class="category-header">
      <el-input v-model="search" placeholder="搜索分类名" style="width: 200px; margin-right: 12px;" @keyup.enter="fetchCategoryList" />
      <el-button type="primary" @click="fetchCategoryList" style="margin-right: 12px;">搜索</el-button>
      <el-button type="primary" @click="openDialog()">新增分类</el-button>
    </div>
    <el-table :data="categoryList" v-loading="loading" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="分类名" />
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template #default="scope">{{ formatDate(scope.row.createTime) }}</template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
        <template #default="scope">{{ formatDate(scope.row.updateTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageSize"
      :total="total"
      layout="total, prev, pager, next, jumper"
      style="margin-top: 16px; text-align: right;"
      @current-change="fetchCategoryList"
      @size-change="fetchCategoryList"
    />
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="400px">
      <el-form :model="form" ref="formRef" label-width="80px">
        <el-form-item label="分类名" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.el-table {
  margin-bottom: 20px;
}
.category-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

/* 新增：让主容器背景透明 */
:deep(.el-table),
:deep(.el-table__header-wrapper),
:deep(.el-table__body-wrapper),
:deep(.el-table th),
:deep(.el-table td),
:deep(.el-table__cell),
:deep(.el-table__row) {
  background: rgba(255, 255, 255, 0.2) !important;
}
:deep(.el-table) {
  background: rgba(255,255,255,0.2) !important;
  border-radius: 16px !important;
  overflow: hidden;
}

:deep(.el-table__header),
:deep(.el-table__header th),
:deep(.el-table thead),
:deep(.el-table thead tr),
:deep(.el-table thead th) {
  background: rgba(255, 255, 255, 0.2) !important;
}

:deep(.el-dialog),
:deep(.el-dialog__body) {
  background: rgb(255, 255, 255) !important;
  border-radius: 0 !important;
}

:deep(.el-pagination) {
  background: rgba(255, 255, 255, 0) !important;
  border-radius: 8px !important;
}

:deep(.el-pager li) {
  background: rgba(255, 255, 255, 0.5) !important;
  border-radius: 6px !important;
}

div:has(> .category-header) {
  background: transparent !important;
  box-shadow: none !important;
}
</style>